<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="changelog.mysql_xdevapi.html">« 更新日志</a></li>
      <li style="float: right;"><a href="ref.mysql-xdevapi.html">Mysql_xdevapi 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.mysql-xdevapi.html">Mysql_xdevapi</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mysql-xdevapi.examples" class="chapter">
 <h1>范例</h1>

 <p class="para">
  The central entry point to the X DevAPI is the <span class="function"><strong>mysql_xdevapi\getSession()</strong></span>
  function, which receives a URI to a MySQL 8.0 Server and returns a
  <span class="classname"><strong class="classname">mysql_xdevap\Session</strong></span> object.
 </p>
 <div class="example" id="example-1546">
  <p><strong>示例 #1 Connecting to a MySQL Server</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$session&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_xdevapi</span><span style="color: #007700">\</span><span style="color: #0000BB">getSession</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysqlx://user:password@host"</span><span style="color: #007700">);<br />}&nbsp;catch(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Connection&nbsp;could&nbsp;not&nbsp;be&nbsp;established:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br />&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;...&nbsp;use&nbsp;$session<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <p class="para">
  The session provides full access to the API. For a new MySQL Server installation,
  the first step is to create a database schema with a collection 
  to store data:
 </p>
 <div class="example" id="example-1547">
  <p><strong>示例 #2 Creating a Schema and Collection on the MySQL Server</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$schema&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createSchema</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$schema</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"example"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <p class="para">
  When storing data, typically <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> is used to encode
  the data into JSON, which can then be stored inside a collection.
 </p>
 <p class="para">
  The following example stores data into the collection we created earlier, 
  and then retrieve parts of it again.
 </p>
 <div class="example" id="example-1548">
  <p><strong>示例 #3 Storing and Retrieving Data</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$marco&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Marco"</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"age"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">19</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"job"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Programmer"<br /></span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$mike&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Mike"</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"age"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">39</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"job"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Manager"<br /></span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$schema&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSchema</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$schema</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"example"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$marco</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mike</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(</span><span style="color: #DD0000">"name&nbsp;=&nbsp;'Mike'"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">fetchOne</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
   <div class="cdata"><pre>
array(4) {
  [&quot;_id&quot;]=&gt;
  string(28) &quot;00005ad66aaf0000000000000003&quot;
  [&quot;age&quot;]=&gt;
  int(39)
  [&quot;job&quot;]=&gt;
  string(7) &quot;Manager&quot;
  [&quot;name&quot;]=&gt;
  string(4) &quot;Mike&quot;
}
</pre></div>
  </div>
 </div>
 <p class="para">
  The example demonstrates that the MySQL Server adds an extra field named
  <code class="code">_id</code>, which serves as primary key to the document.
 </p>
 <p class="para">
  The example also demonstrates that retrieved data is sorted alphabetically.
  That specific order comes from the efficient binary storage inside the MySQL server, but
  it should not be relied upon. Refer to the MySQL JSON datatype documentation for details.
 </p>
 <p class="para">
  Optionally use PHP&#039;s iterators fetch multiple documents:
 </p>
 <div class="example" id="example-1549">
  <p><strong>示例 #4 Fetching and Iterating Multiple Documents</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />foreach&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">${</span><span style="color: #0000BB">doc</span><span style="color: #007700">[</span><span style="color: #DD0000">"name"</span><span style="color: #007700">]}</span><span style="color: #DD0000">&nbsp;is&nbsp;a&nbsp;</span><span style="color: #007700">${</span><span style="color: #0000BB">doc</span><span style="color: #007700">[</span><span style="color: #DD0000">"job"</span><span style="color: #007700">]}</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
   <div class="cdata"><pre>
Marco is a Programmer.
Mike is a Manager.
</pre></div>
  </div>
 </div>
</div>
</div></div></body></html>